const app = getApp()
let collectionLists = []
let page = 0;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        showContact: true,
        suspendShow: true,
        showIcon: true,
        bgcolor: '',
        showIcon: !0,
        myShowToast: false,
        myShowToastImg: '',
        myShowToastCont: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        xhs.stopPullDownRefresh();
        this.setData({
            options: options,
            myPhone: options.myPhone
        })
        let collection = await this.getphonestatusCX();
        collectionLists = collection.gwlb.split(',').map(item => Number(item))
        let str_ggid = collectionLists.splice(0, 10).join(',')
        this.setData({
            collectionLists,
            str_ggid
        })
        this.setData({
            lists: await this.getGG({
                str_ggid
            })
        })
    },
    // 获取收藏列表
    async getphonestatusCX() {
        return new Promise((resolve) => {
            xhs.request({
                url: `https://zg99.offcn.com/index/chaxun/getphonestatus/?actid=54250&callback=&shijian=${Date.parse(new Date())}`,
                method: 'GET',
                dataType: 'jsonp',
                data: {
                    phone: this.data.myPhone
                },
                success: ((res) => {
                    var zjz = res.data.substring(1);
                    zjz = JSON.parse(zjz.substring(0, zjz.length - 1))
                    console.log(zjz)
                    if (zjz.status == 1) {
                        resolve(zjz.user)
                    } else {
                        resolve(false)
                    }
                })
            })
        })
    },
    // 获取收藏列表
    async getGG(data) {
        return new Promise((resolve, reject) => {
            xhs.request({
                url: `https://hbapp.offcn.com/hbqt/web/index.php/Gghz/index/index_ggxxhz_collect?callback=&shijian=${Date.parse(new Date())}`,
                method: 'GET',
                dataType: 'jsonp',
                data,
                success: (res) => {
                    // console.log(res.data)
                    // console.log(JSON.parse(res.data.substr(1, res.data.length - 2)))
                    var lists = JSON.parse(res.data.substr(1, res.data.length - 2)).data.list;
                    let arr_ggid = data.str_ggid.split(',')
                    let newArr1 = arr_ggid.map(item1 => {
                        let arr2 = lists.filter(item2 => item1 == item2.id)
                        return arr2[0]
                    })
                    resolve(newArr1)
                }
            })
        })
    },
    // 打开公告
    gotoArticle: function (e) {
        if (e.currentTarget.dataset.quxiao) {
            this.setData({
                linkgg: e.currentTarget.dataset.url,
                linkid: e.currentTarget.dataset.id,
            })
            xhs.navigateTo({
                url: `/packageB/gg/gg?link=${this.data.linkgg}&id=${this.data.linkid}`,
            })
        }
    },
    //取消关注
    async quxiao (e) {
        let id = e.currentTarget.dataset.id;
        let lists = this.data.lists;
        let str_ggid = this.data.str_ggid;
        let arr_ggid = str_ggid.split(',')
        lists = lists.filter(item => item.id != id)
        str_ggid = arr_ggid.filter(item => item != id).join(',')
        this.setData({
            lists
        })
        let jieguo = await this.updateuserCX(this.data.myPhone, str_ggid)
        if(jieguo == '成功') {
            this.myShowToast('成功', 2000, '取消收藏成功')
        } else {
            this.myShowToast('失败', 2000, '取消收藏失败')
        }
    },
    async updateuserCX(myPhone, bklx) {
        return new Promise((resolve, reject) => {
            xhs.request({
                url: 'https://zg99.offcn.com/index/chaxun/updateuser/?actid=54250&callback=&shijian=' + Date.parse(new Date()) / 1e3,
                method: 'GET',
                dataType: 'jsonp',
                data: {
                    phone: myPhone,
                    gwlb: bklx,
                    isagree: true,
                },
                success(ress) {
                    var zjz = ress.data.substring(1);
                    zjz = JSON.parse(zjz.substring(0, zjz.length - 1))
                    // console.log(zjz)
                    if (zjz.status == 1) {
                        resolve('成功')
                    } else {
                        reject('失败')
                    }
                },
                fail(ress) {
                    reject('失败')
                    // console.log(ress)
                }
            })
        })
    },
    // xhs.showToast 代替版
    myShowToast: function (status, timer, cont) {
        this.setData({
            myShowToast: true,
            myShowToastImg: status == '成功' ? 'https://hb.offcn.com/zg/xcximg/vote/chenggong.png' : status == '失败' ? 'https://hb.offcn.com/zg/xcximg/vote/shibai.png' : 'https://hb.offcn.com/zg/xcximg/vote/loading.png',
            myShowToastCont: cont,
        })
        let that = this;
        setTimeout(function () {
            that.setData({
                myShowToast: false,
            });
        }, timer)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        xhs.hideShareMenu({
            menus: ['shareAppMessage', 'shareTimeline']
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    async onReachBottom() {
        xhs.stopPullDownRefresh();
        if (this.data.collectionLists.length == 0) {
            xhs.showToast({
                title: '没有更多咯',
                icon: 'error',
                duration: 2000
            })
            return false
        }
        let collectionLists = this.data.collectionLists;
        let str_ggid = collectionLists.splice(0, 10).join(',')
        this.setData({
            collectionLists
        })
        console.log(collectionLists)
        this.setData({
            lists: this.data.lists.concat(await this.getGG({
                str_ggid
            }))
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})